/**
 * Copyright (C) 2014-2019 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * <p>
 * Any modifications to this file must keep this entire header intact.
 * <p>
 * Change History - most recent changes go on top of previous changes
 * <p>
 * Comments.java
 * <p>
 * Who   Date       Description
 * ====  =========  =================================================
 * WY    06Nov2015  Initial creation
 */

package com.symaster.common.pixy.meta.image;

import com.symaster.common.pixy.meta.Metadata;
import com.symaster.common.pixy.meta.MetadataEntry;
import com.symaster.common.pixy.meta.MetadataType;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class Comments extends Metadata {
    private final Queue<byte[]> queue;
    private final List<String> comments;

    public Comments() {
        super(MetadataType.COMMENT);
        queue = new LinkedList<byte[]>();
        comments = new ArrayList<String>();
    }

    public Comments(List<String> comments) {
        super(MetadataType.COMMENT);
        queue = new LinkedList<byte[]>();
        if (comments == null) throw new IllegalArgumentException("Input is null");
        this.comments = comments;
    }

    public List<String> getComments() {
        ensureDataRead();
        return Collections.unmodifiableList(comments);
    }

    public void addComment(byte[] comment) {
        if (comment == null) throw new IllegalArgumentException("Input is null");
        queue.offer(comment);
    }

    public void addComment(String comment) {
        if (comment == null) throw new IllegalArgumentException("Input is null");
        comments.add(comment);
    }

    public Iterator<MetadataEntry> iterator() {
        ensureDataRead();
        List<MetadataEntry> entries = new ArrayList<MetadataEntry>();

        for (String comment : comments)
            entries.add(new MetadataEntry(comment, "")); // For comments, we set the value to empty string

        return Collections.unmodifiableCollection(entries).iterator();
    }

    public void read() throws IOException {
        if (queue.size() > 0) {
            for (byte[] comment : queue) {
				comments.add(new String(comment, StandardCharsets.UTF_8));
			}
            queue.clear();
        }
    }
}
